package com.example.demo.controller;

import com.example.demo.result.RestBean;
import com.example.demo.entity.Storage;
import com.example.demo.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;

    @PostMapping("/create")
    public RestBean<Storage> createStorage(@RequestBody Storage storage) {

        int result = storageService.createStorage(storage);

        if (result == 1) {
            System.out.println("添加成功！");
            return RestBean.success(storage);
        }
        System.out.println("添加失败！");
        return RestBean.failure(201,null);
    }

    @PostMapping("/delete")
    public RestBean<String> deleteStorage(@RequestBody Storage storage) {

        int result = storageService.deleteStorage(storage.getSid());

        if (result == 1) {
            return RestBean.success("删除成功！");
        }
        return RestBean.failure(404,"删除失败！");
    }

    @DeleteMapping ("/updelete/{Sid}")
    public RestBean<String> deleteStorage(@PathVariable Integer Sid) {

        storageService.updateIsdeleted(Sid);
        return RestBean.success("仓库删除成功！");
    }

    @PostMapping("/update")
    public RestBean<String> updateStorage(@RequestBody Storage storage) {

        int result = storageService.updateStorage(storage);

        if (result == 1) {
            return RestBean.success("修改成功！");
        }
        return RestBean.failure(201,"修改失败！");
    }

    @PostMapping("/findone")
    public RestBean<List<Storage>> findStorage(@RequestBody Storage storage) {

        List<Storage> stores = new ArrayList<>(5);
        stores.add(storageService.findStorage(storage.getSid()));
        if (stores.size()!=0){
            return RestBean.success(stores);
        }else {
            return RestBean.failure(201,null);
        }
    }


    @GetMapping("/findall")
    public RestBean<List<Storage>> findAllstorages() {

        List<Storage> storages = storageService.findAllstorages();

        if(!storages.isEmpty()) {
            System.out.println("查询成功！");
            return RestBean.success(storages);
        }
        else {
            System.out.println("查询失败！");
            return RestBean.failure(404,null);
        }


    }


}
